package com.amado.course40Reflect.homework;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.reflect.Constructor;

/**
 * 2、打印一个文件夹所有类的构造函数（字符流，加反射）
 * 
 * @author Administrator
 *
 */
public class ConstructorTest {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		File file = new File("D:\\gitCode\\javase\\src\\main\\java\\com\\amado\\course40Reflect");
		for (File tempFile : file.listFiles()) {
			print(tempFile);
		}
	}
	private static void print(File file) throws IOException, ClassNotFoundException {
		String className = getClassName(file);
		if(className!=null) {
			printConstructor(className);
		}
	}
	private static String getClassName(File file) throws IOException {
		if(!file.getName().endsWith(".java")) {
			return null;
		}
		LineNumberReader reader = new LineNumberReader(new FileReader(file));
		String value = reader.readLine();
		reader.close();
		String packageName = getPackageName(value);
		//Goods.java   0 4   Goods 
		String className = file.getName().substring(0, file.getName().indexOf(".java"));
		//com.amado.course40Reflect  + .+  Goods  = com.amado.course40Reflect.Goods
		return packageName+"."+className;
	}
	private static void printConstructor(String className) throws ClassNotFoundException {
		Class<?> clazz = Class.forName(className);
		Constructor<?>[] constructorArray  = clazz.getConstructors();
		for(Constructor<?> constructor:constructorArray) {
			System.out.println(constructor);
		}
	}
	
	private static String getPackageName(String content) {
		if (content != null && content.startsWith("package")) {
			String packageName = content.substring(content.indexOf("package") + "package".length());
			packageName = packageName.substring(0, packageName.length() - 1);
			return packageName.trim();
		}
		return null;
	}
}
